package com.zingrow.web.common.utils;

import java.io.File;

import javax.mail.MessagingException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeUtility;

import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;

public class MailUtil {
    /**
     * 发送普通的文字邮件
     * 
     * @param senderImpl
     *            注解得到的JavaMailSenderImpl对象
     * @param title
     *            邮件标题
     * @param text
     *            邮件正文
     * @param toUserAddress
     *            收件人
     */
    public static void sendMail(final JavaMailSenderImpl senderImpl, String title, String text,
            String[] toUserAddress) {
        try {
            final MimeMessage mimeMessage = senderImpl.createMimeMessage();
            MimeMessageHelper messageHelper = null;
            messageHelper = new MimeMessageHelper(mimeMessage, true, "utf-8");
            messageHelper.setTo(getAddress(toUserAddress));
            String nickName = MimeUtility.encodeText("中周MAC");
            messageHelper.setFrom(new InternetAddress(nickName + "<2104468682@qq.com>"));
            messageHelper.setSubject(title);
            messageHelper.setText(text, true);
            new Thread(new Runnable() {
                @Override
                public void run() {
                    senderImpl.send(mimeMessage);
                }
            }).start();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /*
     * (non Javadoc)
     * 
     * @Title: sendMail
     * 
     * @Description: TODO
     * 
     * @param nickName
     * 
     * @param toUserAddress
     * 
     * @param title
     * 
     * @throws Exception
     * 
     * @see
     * com.zingrow.web.rpt.service.IRptListService#sendMail(java.lang.String,
     * java.lang.String[], java.lang.String)
     */
    public static void sendMail(final JavaMailSenderImpl senderImpl, String nickName, String[] toUserAddress,
            String title, File[] files) throws Exception {
        final MimeMessage mimeMessage = senderImpl.createMimeMessage();
        MimeMessageHelper messageHelper = null;
        try {
            messageHelper = new MimeMessageHelper(mimeMessage, true, "utf-8");
            messageHelper.setTo("67763661@qq.com");
            messageHelper.setFrom("2104468682@qq.com");
            messageHelper.setSubject(title);
            messageHelper.setText("<html><body>" + "<div><div style='line-height: 21px;'>尊敬的领导：</div>"
                    + "<div style='line-height: 21px;'><br></div>"
                    + "<div style='line-height: 21px;'>&nbsp; &nbsp; 请查阅， 谢谢!</div></div>" + "<div><br><br><br></div>"
                    + "<div style='color:#909090;font-family:Arial Narrow;font-size:12px'>------------------</div>"
                    + "<div style='font-size:14px;font-family:Verdana;color:#000;'>"
                    + "<div style='color: rgb(0, 128, 128); font-family: 'lucida Grande', Verdana; font-size: 12px; line-height: 18px;'>"
                    + "<div><font size='2'>温馨提示：因邮件同时发送至多位领导，为避免系统误认为是垃圾邮件，"
                    + "请将我们加入邮件白名单~谢谢合作。</font><br></div></div>"
                    + "<div style='color: rgb(0, 128, 128); font-family: 'lucida Grande', Verdana; font-size: 12px; line-height: 18px;'>"
                    + "<font size='2'><br></font></div>"
                    + "<div style='color: rgb(0, 128, 128); font-family: 'lucida Grande', Verdana; font-size: 12px; line-height: 18px;'>"
                    + "<font size='2'>市场部 - 罗斯</font></div>"
                    + "<div style='color: rgb(0, 128, 128); font-family: 'lucida Grande', Verdana; font-size: 12px; line-height: 18px;'>"
                    + "<font size='2'>------------------------------</font></div>"
                    + "<div style='color: rgb(0, 128, 128); font-family: 'lucida Grande', Verdana; font-size: 12px; line-height: 18px;'>"
                    + "<font size='2'>湖南中周至尚信息技术有限公司</font></div>"
                    + "<div style='color: rgb(0, 128, 128); font-family: 'lucida Grande', Verdana; font-size: 12px; line-height: 18px;'>"
                    + "<font size='2'>QQ：<span style='border-bottom-width: 1px; border-bottom-style: dashed; border-bottom-color: rgb(204, 204, 204); "
                    + "z-index: 1; position: static;' t='7' onclick='return false;' data='2572197802' isout='1'>2572197802</span> &nbsp; "
                    + "Email：<a href='mailto:zzyq2013@qq.com' target='_blank'>zzyq2013@qq.<wbr>com</a></font></div>"
                    + "<div style='color: rgb(0, 128, 128); font-family: 'lucida Grande', Verdana; font-size: 12px; line-height: 18px;'>"
                    + "<font size='2'>Mobile：<span style='border-bottom-width: 1px; border-bottom-style: dashed; border-bottom-color: rgb(204, 204, 204); "
                    + "z-index: 1; position: static;' t='7' onclick='return false;' data='18508430090'>18508430090</span></font></div></div></body></html>",
                    true);

            for (File file : files) {
                String filename = MimeUtility.encodeWord(file.getName());
                messageHelper.addAttachment(filename, file);
            }

            new Thread(new Runnable() {
                @Override
                public void run() {
                    senderImpl.send(mimeMessage);
                }
            }).start();
        } catch (MessagingException e) {
            e.printStackTrace();
        }
    }

    /**
     * 将String类型的邮箱地址转为对象
     * 
     * @param array
     * @return
     */
    private static InternetAddress[] getAddress(String[] array) {
        InternetAddress[] addresses = new InternetAddress[array.length];
        for (int i = 0; i < addresses.length; i++) {
            try {
                addresses[i] = new InternetAddress(MimeUtility.encodeText(array[i]));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return addresses;
    }

    @SuppressWarnings("unused")
    private static String getMailList(String[] mailArray) {

        StringBuffer toList = new StringBuffer();
        int length = mailArray.length;
        if (mailArray != null && length < 2) {
            toList.append(mailArray[0]);
        } else {
            for (int i = 0; i < length; i++) {
                toList.append(mailArray[i]);
                if (i != (length - 1)) {
                    toList.append(",");
                }

            }
        }
        return toList.toString();

    }

}
